package cn.jsj.invite.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.jsj.invite.dao.InvitationDao;
import cn.jsj.invite.dao.StudentDao;
import cn.jsj.invite.exception.CustomerErrorException;
import cn.jsj.invite.pojo.InvitationInfo;
import cn.jsj.invite.pojo.StudentEnt;
import cn.jsj.invite.pojo.identity.UserIdentityEnum;
import cn.jsj.invite.service.EmailService;
import cn.jsj.invite.service.StudentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class StudentServiceImpl implements StudentService {
    private final StudentDao studentDao;
    private final EmailService emailService;
    private final InvitationDao invitationDao;

    @Autowired
    public StudentServiceImpl(StudentDao studentDao, EmailService emailService, InvitationDao invitationDao) {
        this.studentDao = studentDao;
        this.emailService = emailService;
        this.invitationDao = invitationDao;
    }

    @Override
    public Integer registerStudent(StudentEnt studentEnt) {
        String verificationCode = studentEnt.getVerificationCode();
        String mailbox = studentEnt.getMailbox();
        Boolean aBoolean = emailService.checkVerificationCode(verificationCode, mailbox);//判断邮箱验证码
        String studentNumber = studentEnt.getStudentNumber();//获得学生学号
        int schoolId = Math.toIntExact(studentEnt.getSchoolId());//获取学校id
        String account = studentEnt.getAccount();//获取用户名（未注册）
        //邮箱验证码通过 以及 学号、邮箱、用户名判重
        if (aBoolean && studentDao.selectOneStudentByNumber(schoolId, studentNumber) == null && studentDao.selectStudentByMailbox(mailbox) == null && studentDao.selectStudentByAccount(account) == null) {
            return studentDao.insertRegisterStudent(studentEnt);
        }
        return 0;
    }

    @Override
    public Integer updateOneStudentInfo(StudentEnt studentEnt, Long updateId, UserIdentityEnum identityType) {
        if (ObjectUtil.hasNull(studentEnt, updateId, identityType, studentEnt.getId())) {
            return null;
        }
        if (!identityType.equals(UserIdentityEnum.STUDENT)) {
            return -2;
        }
        String mailbox = studentEnt.getMailbox();
        //判断邮箱验证码是否正确、以及新邮箱判重
        if (emailService.checkVerificationCode(studentEnt.getVerificationCode(), mailbox) && (studentDao.selectStudentByMailbox(mailbox) != null)) {
            return studentDao.updateOneStudentInfo(studentEnt);
        } else {
            return -1;
        }
    }

    @Override
    public Integer deleteStudentPageById(int id, int studentId) {
        return studentDao.deleteStudentPageById(id, studentId);
    }

    @Override
    public Integer updateStudentPwdByIdAndOldPwd(String oldPassword, String newPassword, int studentId) {
        return studentDao.updatePasswordByStudentId(oldPassword, newPassword, studentId);
    }

    @Override
    public List<InvitationInfo> listInvitationInfoByStudentId(Integer stuId) {
        List<InvitationInfo> invitationInfos = invitationDao.selectInvitationByStudentId(stuId);
        InvitationServiceImpl.invitationInfoDataFormat(invitationInfos);
        return invitationInfos;
    }

    @Override
    public Integer studentPrivacySwitch(Integer studentId, Integer isOpen) {
        return studentDao.updateOneStudentPrivacy(studentId, isOpen);
    }
}
